

var app = {
   
	// 首页
	blogPage: {

    pageContainer: $('#blogList'),

		init: function(){
        var self = this;
        this.router = new Router({
            root: '/blog',
            routes: {
                '': function(){
                  self.getTagList();
                  self.getBlogList();
                },

                '/': function(){
                  self.getTagList();
                  self.getBlogList();
                },

                '/tag/:id': function(tag){
                  self.getBlogList({tag: tag});
                  self.getTagList(tag);
                },

                '/detail/:id': function(id){
                  self.getTagList();
                  self.getDetail({id: id});
                }
            }
        });

        this.router.start();
		},

    // 获取博客列表
    getBlogList: function(data){
        var self = this;
        server.ajax({
            url: 'GET_BLOG',
            data: data,
            success: function(res){
                self.renderBlog(res.data);
            }
        })
    },

    getDetail: function(data){
        var self = this;
        server.ajax({
            url: 'GET_DETAILS',
            data: data,
            success: function(res){
                self.renderDetail(res.data);
            }
        })
    },

    // 获取tag列表
    getTagList: function(id){
      var self = this;
      server.ajax({
        url: 'GET_TAG',
        success: function(res){
            if (id) res.data.active = id;
            self.renderTag(res.data);
        }
      })
    },

		renderBlog: function(data){
        var blog = template.render(blogTpl)({list: data, type: 'list'});
        $("#blogList").html(blog);
		},

    renderDetail: function(data){
        var html = template.render(blogTpl)({list: [data], type: 'detail'});
        $("#blogList").html(html);
    },

    renderTag: function(data){
        var html = template.render(tagTpl)({list: data});
        $("#tagList").html(html);
    }
	},

  collectionPage: {
      init: function(){
          this.getList();
      },

      getList: function(){
          var self = this;
          server.ajax({
            url: 'GET_COLLECTS',
            success: function(res){
                self.renderCollects(res.data);
            }
          })
      },

      renderCollects: function(data){
           var html = template.render(collectsTpl)({list: data});
           $("#collects").html(html);
      }
  }

}

var tagTpl = ''+
    ' <a class="mdl-navigation__link {{if !list.active}} active {{/if}}" href="/blog/">所有</a>'+
    '{{each list as item }}'+
    '   <a class="mdl-navigation__link {{if list.active == item.name}} active {{/if}}" href="/blog/tag/{{item.name}}">{{item.name}}</a>'+
    '{{/each}}';

var blogTpl = ''+
    '{{each list as item }}'+
    '<section class="section--center mdl-grid mdl-grid--no-spacing mdl-shadow--2dp">'+
    '  <div class="mdl-card mdl-cell mdl-cell--12-col">'+
    '    <div class="mdl-card__supporting-text">'+
    '      <h4>{{item.title}}</h4>'+
    '       {{if type == "list"}}'+
    '         {{#item.description || "这家伙很懒，什么都没写"}}…'+
    '         <br/><br/><a href="/blog/detail/{{item._id}}" class="mdl-button mdl-js-button mdl-button--raised item-content-btn">详情…</a>'+
    '       {{else}}'+
    '         {{#item.content}}'+
    '       {{/if}}'+
    '       </div>'+
    '     </div>'+
    '   <div class="mdl-card__supporting-text meta mdl-color-text--grey-600">'+
    '    <div class="minilogo">'+
    '      <img class="userPhoto" src="/static/imgs/{{item.user.photo}}.png">'+
    '    </div>'+
    '    <div class="user-desc">'+
    '      <p>{{item.user.nickname}}</p>'+
    '      <p>{{item.createTime | creatTime}}</p>'+
    '    </div>'+
    '  </div> '+
    '</section>'+
    '{{/each}}';

var collectsTpl = ''+
    '{{each list as item}}'+
    '<section class="section--center mdl-grid mdl-grid--no-spacing mdl-shadow--2dp">'+
    '  <div class="mdl-card mdl-cell mdl-cell--12-col">'+
    '    <div class="mdl-card__supporting-text">'+
    '      <h4>{{item.createTime | creatTime}} - {{item.title}}</h4>'+
    '      {{item.text}}'+
    '    </div>'+
    '    <div class="mdl-card__actions">'+
    '      <a href="{{item.url}}" target="_blank"  class="mdl-button portal"> <i class="material-icons">arrow_forward</i> 传送门</a>'+
    '    </div>'+
    '  </div>'+
    '</section>'+
    '{{/each}}';


template.helper('creatTime', function (time) {
  var d = new Date(parseInt(time));
  var ds = d.getFullYear()+'/'+(d.getMonth()+1)+'/'+d.getDate();
  return ds;
});


